//
//  PlayerViewController.m
//  UI_项目 测试1
//
//  Created by dlios on 15-1-17.
//  Copyright (c) 2015年 songCheng. All rights reserved.
//

#import "PlayerViewController.h"
#import "DynamicTableViewCell.h"
#import "DynamicViewController.h"
#import "PlayerSingle.h"
@interface PlayerViewController ()

@end

@implementation PlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.title1;
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    image.image = [UIImage imageNamed:@"playerbj.jpg"];
    
    [self.view addSubview:image];
    
    UIImageView *imageSmall = [[UIImageView alloc] initWithFrame:CGRectMake(30,120, 315, 350)];
    
    imageSmall.image = self.image.image;
    
//    imageSmall.alpha = 0.6;
    
    imageSmall.layer.cornerRadius = 20;
    
    imageSmall.layer.masksToBounds = YES;
    
  
    [self.view addSubview:imageSmall];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 607, 375, 60)];
    
    label.backgroundColor = [UIColor orangeColor];
    
    label.alpha = 0.5;
    
    [self.view addSubview:label];
    
    // 上一首
    self.buttonTop = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.buttonTop.frame = CGRectMake(110, 617, 30, 30);
    
    self.buttonTop.layer.cornerRadius = 15;
    
    self.buttonTop.layer.masksToBounds = YES;
    
    self.buttonTop.backgroundColor = [UIColor blueColor];
    
//    [self.buttonTop setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    
    [self.view addSubview:self.buttonTop];
    // 播放
    self.buttonPlay = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.buttonPlay.frame = CGRectMake(185, 617, 30, 30);
    
    self.buttonPlay.backgroundColor = [UIColor blueColor];
    
    [self.buttonTop addTarget:self action:@selector(buttonPlay:) forControlEvents:UIControlEventTouchUpInside];
    
//    [self.buttonPlay setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    
    [self.view addSubview:self.buttonPlay];
    
    // 下一首
    self.buttonNext = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.buttonNext.frame = CGRectMake(255, 617, 30, 30);
    
    self.buttonNext.backgroundColor = [UIColor blueColor];
    
//    [self.buttonNext setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    
    [self.view addSubview:self.buttonNext];
    
    
    
    NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.text1]];
    
    self.player = [[AVAudioPlayer alloc] initWithData:mydata error:nil];
    
    self.player.delegate = self;
    
//    [_player prepareToPlay];
    
   
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 600, 375, 20)];
    
    self.slider.maximumValue = self.player.duration;
    
    [self.slider addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:self.slider];
    
    [self.slider release];
    
    _time = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateslider) userInfo:nil repeats:YES];
    
    UISwitch *loop = [[UISwitch alloc] initWithFrame:CGRectMake(320, 625, 30, 20)];
    
    [loop addTarget:self action:@selector(loopYesOrNo) forControlEvents:UIControlEventValueChanged];
    
    loop.on = NO;
    
    [self.view addSubview:loop];
    
    [loop release];
    
    self.volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(5, 630, 50, 20)];
    
    [self.volumeSlider addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged];
    
    self.volumeSlider.minimumValue = 0.0f;
    
    self.volumeSlider.maximumValue = 1.0f;
    
    self.volumeSlider.value = 0.5f;
    
    [self.view addSubview:self.volumeSlider];
    
    
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 625, 30, 20)];
    
    [self.view addSubview:self.timeLabel];
    
}


- (void)buttonPlay:(UIButton *)button
{

    if (self.staus == playStatusYes) {
        self.staus = playStatusNo;
        [_player pause];
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateMusicTimeLabel) userInfo:self repeats:YES];
        _buttonPlay.backgroundColor = [UIColor redColor];
    }else{
    
        [_player play];
        
        self.staus = playStatusYes;
        
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateMusicTimeLabel) userInfo:self repeats:YES];
        
        _buttonPlay.backgroundColor = [UIColor blueColor];
    
    }

}

- (void)updateMusicTimeLabel
{

    if ((int)self.player.currentTime % 60 < 10) {
        self.timeLabel.text = [NSString stringWithFormat:@"%d:0%d",(int)self.player.currentTime / 60, (int)self.player.currentTime % 60];
    }else{
    
      self.timeLabel.text = [NSString stringWithFormat:@"%d:%d",(int)self.player.currentTime / 60, (int)self.player.currentTime % 60];
    
    }

}

- (void)loopYesOrNo
{

    _player.numberOfLoops = -1;
}
- (void)updateslider
{

    self.slider.value = self.player.currentTime;

}

- (void)sliderValueChanged
{

    [self.player stop];
    
    [self.player setCurrentTime:self.slider.value];
    
    [self.player prepareToPlay];
    
    [self.player play];


}

- (void)volumeChange

{

    _player.volume = _volumeSlider.value;

}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{

    self.staus = playStatusNo;
    
    [_player pause];
    
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateMusicTimeLabel) userInfo:self repeats:YES];
    
    _buttonPlay.backgroundColor = [UIColor redColor];


}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{

    [_player play];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];


}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
